package com.hstc.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.hstc.order.bean.Order;
import com.hstc.properties.OrderProperties;
import com.hstc.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

//@RefreshScope//配置属性自动刷新
//@RequestMapping("/api/order")
@RestController
public class OrderController {

    @Autowired
    OrderService orderService;

    @Autowired
    OrderProperties orderProperties;

//    @Value("${order.timeout}")
//    String orderTimeout;
//    @Value("${order.auto-confirm}")
//    String orderAutoConfirm;
    @GetMapping("/config")
    public String config(){
        return "order.timeout="+orderProperties.getTimeout()+",order.auto-confirm="+orderProperties.getAutoConfirm()+",db-url="+orderProperties.getDbUrl();
    }

    @GetMapping("/create")
    public Order getProduct(@RequestParam("userId") Long userId,
                            @RequestParam("productId") Long productId){
        Order order = orderService.createOrder(productId, userId);
        return order;
    }

    @GetMapping("/seckill")
    @SentinelResource(value = "seckill-order",blockHandler = "seckillFallback")
    public Order seckill(@RequestParam("userId") Long userId,
                            @RequestParam("productId") Long productId){
        Order order = orderService.createOrder(productId, userId);
        return order;
    }

    public Order seckillFallback(Long userId, Long productId, BlockException exception){
        Order order = new Order();
        order.setId(productId);
        order.setUserId(userId);
        order.setAddress("异常处理"+exception.getClass());
        return order;
    }

    @GetMapping("/readDB")
    public String readDB(){
        return "readDB 数据表1";
    }
    @GetMapping("/writeDB")
    public String writeDB(){
        return "writeDB 数据表1";
    }
}
